/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>广告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索:">
        <el-input
          size="small"
          v-model="formInline.pmAdvertising.head"
          placeholder="想要查询的广告标题"
        ></el-input>
      </el-form-item>
      <el-form-item >
        <el-input
          size="small"
          v-model="formInline.pmAdvertising.text"
          placeholder="想要查询的广告内容"
        ></el-input>
      </el-form-item>
      <el-form-item >
        <el-input
          size="small"
          v-model="formInline.pmAdvertising.status"
          placeholder="查询广告的状态"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="广告编号" width="100">
      </el-table-column>
      <el-table-column sortable prop="head" label="广告标题" width="200">
      </el-table-column>
      <el-table-column sortable prop="text" label="广告内容" width="200">
      </el-table-column>
      <el-table-column sortable prop="createTime" label="发布时间" width="200">
      </el-table-column>
      <el-table-column sortable prop="status" label="状态" width="100">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.status==='未发布'">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)" v-if="scope.row.status==='发布'">删除</el-button>
          <el-button size="mini" @click="status(scope.$index, scope.row)" v-if="scope.row.status==='未发布'">发布</el-button>
          <el-button size="mini" type="danger" @click="disstatus(scope.$index, scope.row)" v-if="scope.row.status==='发布'">取消发布</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑/增加界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <el-form
        label-width="120px"
        :model="userFrom"
        :rules="rules"
        ref="userFrom"
      >
        <el-form-item label="标题" prop="head">
          <el-input
            size="small"
            v-model="userFrom.head"
            auto-complete="off"
            placeholder="请输入广告标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="text">
          <el-input
            size="small"
            v-model="userFrom.text"
            auto-complete="off"
            placeholder="请输入广告内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="发布时间" prop="createTime">
          <el-date-picker
            type="datetime"
            size="small"
            v-model="userFrom.createTime"
            auto-complete="off"
            placeholder="请选择发布时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
     
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('userFrom')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import * as advertising from "../../api/advertising";
import axios from "axios";

export default {
  //model
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "添加",
      userFrom: {
        id:"",
        head:"",
        text:"",
        createTime:"",
        status:""
      },
      // rules表单验证
      rules: {
        // deptName: [
        //   { required: true, message: '请输入部门名称', trigger: 'blur' }
        //  ],
        //  deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 5,
        pmAdvertising: {
        head:"",
        text:"",
        status:""
        },
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 5,
        total: 10,
      },

      //下拉框属性
      options: [],
      value: "",
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getDate();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {

    getDate() {
      advertising.selectAdvertising(this.formInline.page,this.formInline.limit).then((res) =>{
        if(res.data.code == 200){
          this.listData = res.data.data.list;
          this.pageparm.total = res.data.data.total;
        }
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.search();
    },
    // 搜索事件
    search() {
      advertising.selectMH(this.formInline).then((res) =>{
        if(res.data.code == 200){
          this.listData = res.data.data.list;
          this.pageparm.total = res.data.total;
        }
      })

    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.userFrom.id = row.id;
        this.userFrom.head = row.head;
        this.userFrom.text = row.text;
        this.userFrom.status = row.status;
        this.userFrom.createTime = row.createTime;
      } else {
        this.title = "添加";
        this.userFrom.head = "";
        this.userFrom.text = "";
        this.userFrom.createTime ="";
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          //下面这个是 系统封装好的接口方法!
          // deptSave(this.userFrom)
          if (this.title == "添加") {
            advertising.insert(this.userFrom).then((res)=>{
              this.editFormVisible = false;
              this.loading = false; //加载状态
              if(res.data.code==200){
                  this.$message({
                  type: "success",
                  message: "添加广告成功！",
                  });
                  this.getDate();
              }
            })
          } else if (this.title == "修改") {
            advertising.update(this.userFrom).then((res)=>{
              this.editFormVisible = false;
              this.loading = false; //加载状态
              if(res.data.code==200){
                  this.$message({
                  type: "success",
                  message: "更改广告成功！",
                  });
                  this.getDate();
              }
            })
          }
        }
      });
    },
    status(index,row){
      advertising.updateStatus(row).then((res) =>{
        if(res.data.code == 200){
          this.$message({
          type: "success",
          message: "发布成功！",
          });
          this.getDate();
        }
      })
    },
    disstatus(index,row){
      advertising.updateDisStatus(row).then((res) =>{
        if(res.data.code == 200){
          this.$message({
          type: "success",
          message: "已取消发布！",
          });
          this.getDate();
        }
      })
    },

    // 删除角色
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })   
        .then(() => {
          advertising.deleted(row.id).then((res)=>{
          if(res.data.code == 200){
            this.$message({
            type: "success",
            message: "广告已删除!", 
            })
            this.getDate();
          }
          else{
            this.$message({
            type: "error",
            message: "广告删除失败!", 
            })
          }
        })


        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 